package com.chocho.producerserver.controller;

import com.chocho.producerserver.service.TestService;
import com.chocho.producerserver.utils.Msg;
import com.chocho.producerserver.vo.VQuestion;
import com.chocho.producerserver.vo.VTest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * Created by chocho on 2022/4/12.
 */
@CrossOrigin
@RestController
@RequestMapping("/test")
@PreAuthorize("hasAuthority('test')")
public class TestController {

    private Logger logger = LoggerFactory.getLogger(TestController.class);

    @Autowired
    private TestService testService;

    @PostMapping("/add")
    @ResponseBody
    public Msg addTest(HttpServletRequest request, @RequestBody VTest v){
        return testService.addTest(request, v) ? Msg.success() : Msg.fail();
    }

    @PostMapping("/select")
    @ResponseBody
    public Msg selectTest(@RequestBody Map<String, Object> map){
        List<VTest> testList = testService.getTestByQuery(map);
        return (testList != null)
                ? Msg.success().add("testList", testList)
                : Msg.fail();
    }

    @GetMapping("/delete")
    @ResponseBody
    public Msg deleteTest(String ids){
        if(ids != null && !"".equals(ids)){
            return testService.deleteTest(ids) ? Msg.success() : Msg.fail();
        }
        return Msg.fail();
    }

    @PostMapping("/update")
    @ResponseBody
    public Msg updateTest(@RequestBody VTest v){
        return testService.updateTest(v) ? Msg.success() : Msg.fail();
    }
}
